package org.opensourceway.sbom.clients.vul;

import org.opensourceway.sbom.api.vul.MajunVulClient;
import org.opensourceway.sbom.model.pojo.response.vul.uvp.MajunVulDetailsResponse;
import org.opensourceway.sbom.utils.WebUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;

@Service
public class MajunVulClientImpl implements MajunVulClient {

    @Autowired
    private WebUtil webUtil;

    @Value("${majun.vul.api.url}")
    private String defaultBaseUrl;

    @Value("${majun.api.token}")
    private String token;

    @Override
    public Mono<MajunVulDetailsResponse> getMajunVulDetails() {
        WebClient client = webUtil.createWebClient(defaultBaseUrl);

        return client.get()
                .uri("/api/http/majun-vulnerability-view/admin/ci-portal/ci-admin/cve/details")
                .header("access_token",token)
                .retrieve()
                .bodyToMono(MajunVulDetailsResponse.class)
                .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)));
    }
}
